package com.eedi.org.healthinfo.service;

import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.collection.CollectionUtils;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.org.healthinfo.controller.vo.OrgHealthInfoGroupPageReqVO;
import com.eedi.org.healthinfo.controller.vo.OrgHealthInfoGroupSaveReqVO;
import com.eedi.org.healthinfo.dal.dataobject.OrgHealthInfoDO;
import com.eedi.org.healthinfo.dal.dataobject.OrgHealthInfoGroupDO;
import com.eedi.org.healthinfo.dal.mapper.OrgHealthInfoGroupMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.org.enums.OrgErrorCodeConstants.HEALTH_INFO_GROUP_SAME_NAME_EXIST;
import static com.eedi.org.enums.OrgErrorCodeConstants.HEALTH_INFO_GROUP_APPLY_AVOID_DEL;
import static com.eedi.org.enums.OrgErrorCodeConstants.HEALTH_INFO_GROUP_NOT_EXISTS;
import static com.eedi.org.enums.OrgErrorCodeConstants.HEALTH_INFO_GROUP_APPLY_AVOID_DISABLE;

/**
 * 健康资讯分类管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrgHealthInfoGroupServiceImpl implements OrgHealthInfoGroupService {

    @Resource
    private OrgHealthInfoGroupMapper orgHealthInfoGroupMapper;

    @Resource
    private OrgHealthInfoService orgHealthInfoService;

    @Override
    public String createHealthInfoGroup(OrgHealthInfoGroupSaveReqVO createReqVO) {
        //根据名称判重，不允许同名插入
        if (orgHealthInfoGroupMapper.selectByName(createReqVO.getOrgGroupName()) != null) {
            throw exception(HEALTH_INFO_GROUP_SAME_NAME_EXIST);
        }
        // 插入
        OrgHealthInfoGroupDO healthInfoGroup = BeanUtils.toBean(createReqVO, OrgHealthInfoGroupDO.class);
        orgHealthInfoGroupMapper.insert(healthInfoGroup);
        // 返回
        return healthInfoGroup.getOrgHealthInfoGroupId();
    }

    @Override
    public void updateHealthInfoGroup(OrgHealthInfoGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateHealthInfoGroupExists(updateReqVO.getOrgHealthInfoGroupId());

        OrgHealthInfoGroupDO existGroup = orgHealthInfoGroupMapper.selectByName(updateReqVO.getOrgGroupName());
        if (existGroup != null && !existGroup.getOrgHealthInfoGroupId().equals(updateReqVO.getOrgHealthInfoGroupId())) {
            throw exception(HEALTH_INFO_GROUP_SAME_NAME_EXIST);
        }
        // 更新
        OrgHealthInfoGroupDO updateObj = BeanUtils.toBean(updateReqVO, OrgHealthInfoGroupDO.class);
        orgHealthInfoGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteHealthInfoGroup(String orgHealthInfoGroupId) {
        // 校验存在
        validateHealthInfoGroupExists(orgHealthInfoGroupId);
        //校验该分类是否被应用
        List<OrgHealthInfoDO> healthInfoDOS = orgHealthInfoService.getHealthInfoByGroupId(orgHealthInfoGroupId);
        if(!CollectionUtils.isAnyEmpty(healthInfoDOS)){
            throw exception(HEALTH_INFO_GROUP_APPLY_AVOID_DEL);
        }
        // 删除
        orgHealthInfoGroupMapper.deleteById(orgHealthInfoGroupId);
    }

    @Override
    public List<OrgHealthInfoGroupDO> listHealthInfoGroup(CommonStatusEnum status) {
        return orgHealthInfoGroupMapper.selectList(new LambdaQueryWrapperX<OrgHealthInfoGroupDO>()
                .eqIfPresent(OrgHealthInfoGroupDO::getOrgStatus, status));
    }

    private void validateHealthInfoGroupExists(String orgHealthInfoGroupId) {
        if (orgHealthInfoGroupMapper.selectById(orgHealthInfoGroupId) == null) {
            throw exception(HEALTH_INFO_GROUP_NOT_EXISTS);
        }
    }


    @Override
    public PageResult<OrgHealthInfoGroupDO> getHealthInfoGroupPage(OrgHealthInfoGroupPageReqVO pageReqVO) {
        return orgHealthInfoGroupMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateHealthInfoGroupStatus(String orgHealthInfoGroupId, CommonStatusEnum status) {
        // 校验存在
        validateHealthInfoGroupExists(orgHealthInfoGroupId);

        if (CommonStatusEnum.DISABLE.equals(status)) {
            List<OrgHealthInfoDO> healthInfoDOS = orgHealthInfoService.getHealthInfoByGroupId(orgHealthInfoGroupId);
            if (!CollectionUtils.isAnyEmpty(healthInfoDOS)) {
                throw exception(HEALTH_INFO_GROUP_APPLY_AVOID_DISABLE);
            }
        }
        // 更新状态
        OrgHealthInfoGroupDO updateObj = new OrgHealthInfoGroupDO();
        updateObj.setOrgHealthInfoGroupId(orgHealthInfoGroupId);
        updateObj.setOrgStatus(status);
        orgHealthInfoGroupMapper.updateById(updateObj);
    }

}
